;(function($){
	this.myInfo = function(){
		var infoChange;//个人资料页面的每个选项后的修改按钮
		var facetDis;//个人资料页面的头像图片
		var addressBlock;//地址管理页面的地址块
		var changeAddr;//地址块上的修改按钮
		var deleteAddrBtn;//地址块上的删除按钮
		var confirmDelBtn;//地址块上的删除按钮点击后出现的是否删除按钮
		var addressDialog;//修改地址或增加地址时弹出框
		var dialogClose;//修改地址或增加地址时弹出框的右上角关闭按钮或取消按钮
		var addAddressBtn;//地址管理页面最右边的的增加地址
		var fillInput;//修改密码页面的input
		var errorShow;//修改密码页面的input下的错误提示span
		var newpwdReg = /^([\w]{6,20})$/;
		var submitBtn;
		var linkChange;//安全中心的右侧各项修改按钮（更改密码、更改手机、绑定邮箱、更改额度）
		var changeInput;//更改手机、绑定邮箱、更改额度页面的input框
		
		var initData = function(){
			//ajax接收用户密码
		};
		
		var loadEvent = function(){
			infoChange.on('click',jumpChange);//个人资料页面的每个选项后的修改按钮，点击后跳转各子项
			
			//移入用户头像出现编辑头像提示
			faceDis.hover(function(){
				$(this).find('.profileinfo-facedit').show();
			},function(){
				$(this).find('.profileinfo-facedit').hide();
			});
			
			//移入每个地址块时button文本变蓝
			addressBlock.hover(function(){
				$(this).find('.desktop-addressblock-buttons button').css('color','#0089dc');
			},function(){
				$(this).find('.desktop-addressblock-buttons button').css('color','#bbb');
			})
			
			changeAddr.on('click',showChangeInfo);//点击修改按钮跳出修改框
			
			addAddressBtn.on('click',showChangeInfo)//点击增加按钮跳出增加地址框
			
			deleteAddrBtn.on('click',deleteAddr);//删除地址
			
			//关闭修改地址框
			dialogClose.on('click',function(){
				addressDialog.hide();
				$('.blackbox').remove();
			})
			
			confirmDelBtn.on('click',confirmDel);//是否删除地址
			
			linkChange.on('click',jumpAct);//安全中心点击修改时各项跳转
			
			//修改密码文本框失去焦点时验证
			fillInput.on('blur',function(){
				testData(this);
			});
			
			//修改密码页面的提交按钮点击时验证
			submitBtn.on('click',function(){
				for(var i = 0; i < fillInput.length; i++){
					testData(fillInput[i]);
				}
				if(fillInput[0].err && fillInput[1].err && fillInput[2].err){
					alert('success');
					//提交数据
				}else{
					return false;//阻止提交
				}
			});
			
			changeInput.on('focus',showTips1);//更改手机、绑定邮箱、更改额度页面的input框聚焦时提示
			
			changeInput.on('blur',showTips2);//更改手机、绑定邮箱、更改额度页面的input框失焦时提示
			
		};
		
		//个人资料点击跳转到各子项
		var jumpChange = function(){
			if(this === infoChange[0]){
				$('body').append('<div class="blackbox"></div>');
				$('.dialog').show();
				$('.dialog-close.icon-close , .modifyname-cancel').on('click',function(){
					$('.dialog').hide();
					$('.blackbox').remove();
				});
			}else{
				$('.mainshow.info').hide();
				$('.mainshow.security').show();
			}
		}
		
		//弹出修改地址或增加地址框
		var showChangeInfo = function(){
			$('body').append('<div class="blackbox"></div>');
			addressDialog.show();
		}
		
		//点击删除地址时弹出蒙层
		var deleteAddr = function(){
			var _parent = $(this).parents('.desktop-addressblock');
			_parent.find('.desktop-addressblock-removebuttons').removeClass('ng-hide');
			_parent.find('.desktop-addressblock-mask').removeClass('ng-hide');
			
		}
		
		//确认是否删除该地址
		var confirmDel = function(){
			var _parent = $(this).parents('.desktop-addressblock');
			if($(this).hasClass('confirmdelete')){
				$(this).parents('.desktop-addressblock').remove();
				//后台删除地址
			}else{
				_parent.find('.desktop-addressblock-removebuttons').addClass('ng-hide');
				_parent.find('.desktop-addressblock-mask').addClass('ng-hide');
			}
		}
		
		//安全中心点击跳转到各子项
		var jumpAct = function(){
			$(this).parents('.security.mainshow').hide();
			switch (this){
				case linkChange[0]:
					$('.changepassword.mainshow').show();//弹出修改密码层
				return;
				case linkChange[1]:
					$('.changemobile.nextshow').show();//弹出修改手机号层
				return;
				case linkChange[2]:
					$('.bindemail.nextshow').show();//弹出绑定邮箱层
				return;
				case linkChange[3]:
					$('.modifypay.nextshow').show();//弹出修改额度层
				return;
			}
		}
		
		var testData = function(_node){
			if(_node === fillInput[0]){
				if(newpwdReg.test(_node.value) && !_node.value.match('_')){
					showRight(0);
				}else{
					showError(0);
					$(errorShow[0]).html('当前密码错误，请重新输入');
				}
			}else if(_node === fillInput[1]){
				if(newpwdReg.test(_node.value) && !_node.value.match('_')){
					showRight(1);
				}else{
					showError(1);
					$(errorShow[1]).html('密码需要是字母或数字，最小6位，最大20位');
				}
				newPwd = _node.value;
			}else{
				if(_node.value === ''){
					showError(2);
					$(errorShow[2]).html('请再次输入密码');
				}else if(_node.value !== $(fillInput[1]).val()){
					showError(2);
					$(errorShow[2]).html('两次密码输入不一致，请重新输入');
				}else{
					showRight(2);
				}
			}
		};
		
		var showError = function(index){
			$(fillInput[index]).css('border-color','#ff464c');
			$(errorShow[index]).addClass('icon-dot-error');
			fillInput[index].err = false;
		}
		
		var showRight = function(index){
			$(fillInput[index]).css('border-color','#dedede');
			$(errorShow[index]).removeClass('icon-dot-error').html('');
			fillInput[index].err = true;
		}
		
		var showTips1 = function(){
			var _parent = $(this).parents('.formfield');
			_parent.removeClass('validate-error');
			_parent.find('span:eq(0)').removeClass('icon-dot-error').addClass('icon-dot-warning');
			_parent.find('span:eq(0)').html($(this).prev().html() + '是必填项');
		}
		
		var showTips2 = function(){
			var _parent = $(this).parents('.formfield');
			_parent.addClass('validate-error');
			_parent.find('span:eq(0)').removeClass('icon-dot-warning').addClass('icon-dot-error');
			_parent.find('span:eq(0)').html('请输入正确的' + $(this).prev().html());
		}

		return {
			run: function(){
				infoChange = $('.profileinfo-link');
				faceDis = $('.info .profileinfo-face');
				addressBlock = $('.desktop-addressblock');
				changeAddr = $('.desktop-addressblock-buttons button:first-of-type');
				deleteAddrBtn = $('.desktop-addressblock-buttons button:last-of-type');
				confirmDelBtn = $('.desktop-addressblock-removebuttons button');
				dialogClose = $('.addressdialog-close,.addressform-buttons button:last-of-type');
				addressDialog = $('.addressdialog');
				addAddressBtn = $('.desktop-addressblock.desktop-addressblock-addblock');
				fillInput = $('.changepwd input');
				errorShow = $('.changepwd .formfield-hint span:first-of-type')
				submitBtn = $('[type=submit]');
				linkChange = $('a.security-type-link');
				changeInput = $('.changemobile input,.bindemail input,.modifypay input');
				initData();
				loadEvent();
			}
		}
	}();

})(jQuery);
